<template>
	<view class="main">
		<view class="list" v-for="(item,index) in list" :key="index">
			<view class="name flex align between">
				<view>{{item.title}}</view>
				<view>{{item.addtime}}</view>
			</view>
			<view class="flex align between info">
				<video :src="item.video_url" controls></video>
				<view class="info-item">
					<view class="flex align between">
						<view>视频类型</view>
						<view>{{item.type==1?'普通视频':'混剪视频'}}</view>
					</view>
					<view class="flex align between">
						<view>时长(S)</view>
						<view>{{item.duration}}</view>
					</view>
					<view class="flex align between">
						<view>大小(M)</view>
						<view>{{item.file_size}}</view>
					</view>
				</view>
				<uni-icons type="trash" size="20" color="#D81E06"></uni-icons>
			</view>
			<view class="list-btn" @click="onStartDownload(item.video_url)">保存到手机相册</view>
		</view>
		<xw-empty :isShow="empty" text="暂无数据" textColor="#777777"></xw-empty>
		<uni-load-more :status="status" v-if="!empty" :content-text="loadText"></uni-load-more>
	</view>
</template>

<script>
	import {
		tuiguangVideoIndex
	} from '@/api/apiData.js';
	export default {
		data() {
			return {
				totalPage: 1,
				page: 0,
				offset: 20,
				list: [],
				scrollTop: 0,
				empty: false,
				loadText: {
					loadmore: '点击或上拉加载更多',
					loading: '努力加载中',
					nomore: '实在没有了'
				},
				status: 'loadmore',
			};
		},
		onLoad(option) {
			this.group_ids = option.id
			this.getlist();
		},
		onShow() {
			// this.page = 0
			// this.list = []
			// this.getlist();
		},
		onPageScroll(e) {
			const that = this;
			that.scrollTop = e.scrollTop;
		},
		onReachBottom() {
			const that = this;
			that.getlist();
		},
		methods: {
			onStartDownload(url) {
				wx.getSetting({
					success: (res) => {
						if (res.authSetting['scope.writePhotosAlbum'] === undefined) {
							wx.authorize({
								scope: 'scope.writePhotosAlbum',
								success: () => {
									this.downVideo(url)
								},
								fail: (err) => {
									console.log('授权失败:', err)
								}
							})
						} else if (!res.authSetting['scope.writePhotosAlbum']) {
							wx.openSetting({
								success: (res) => {
									if (res.authSetting['scope.writePhotosAlbum']) {
										this.downVideo(url)
									}
								}
							})
						} else {
							this.downVideo(url)
						}
					}
				})
			},
			downVideo(url) {
				let that = this
				uni.showLoading({
					title: '保存中...',
				})
				uni.downloadFile({
					//视频信息的Url
					url: url,
					success: function(res) {
						// 只要服务器有响应数据，就会把响应内容写入文件并进入 success 回调，业务需要自行判断是否下载到了想要的内容
						if (res.statusCode === 200) {
							console.log(res.tempFilePath);
							uni.saveVideoToPhotosAlbum({
								filePath: res.tempFilePath,
								success: function(res) {
									console.log(res.errMsg)
									uni.hideLoading();
									uni.showToast({
										title: '已保存到相册'
									})
								},
								fail() {
									uni.showToast({
										title: '保存失败',
										icon: 'error'
									})
									uni.hideLoading();
								}
							})
						}
					},
					fail() {
						uni.showToast({
							title: '保存失败',
							icon: 'error'
						})
						uni.hideLoading();
					}
				})
			
			},
			async getlist() {
				const that = this;
				const {
					totalPage,
					page,
					offset,
					list,
					group_id,
					type,
					searchValue,
					group_ids
				} = that;
				let obj = {
					menu_url: 'tuiguang_video',
					limit: offset,
					offset: page,
					sort: 'id',
					make_video_id: group_ids
					// op:encodeURIComponent(JSON.stringify(filter))
				};
				that.status = 'loading';
				if (totalPage > page) {
					let result = await tuiguangVideoIndex(obj);
					that.page = page + 1;
					if (result.total == 0) {
						that.empty = true;
						that.status = 'nomore';
						return;
					}
					that.empty = false;
					setTimeout(() => {
						that.status = 'loadmore';
					}, 500);
					that.list = list.concat(result.rows);
					that.totalPage = Math.ceil(result.total / offset);
				} else {
					setTimeout(() => {
						that.status = 'nomore';
					}, 500);
				}
			},
		}
	}
</script>

<style lang="scss" scoped>
	.main {
		margin: 13rpx 30rpx;

		.list {
			border-radius: 15rpx;
			background: white;
			box-shadow: 1rpx 1rpx 5rpx 1rpx gainsboro;
			margin-top: 20rpx;
			padding-bottom: 10rpx;
			.list-btn{
				background: black;
				color: white;
				font-size: 24rpx;
				width: fit-content;
				margin: 20rpx auto;
				padding: 10rpx 40rpx;
				border-radius: 10rpx;
			}

			.name {
				margin: 0 17rpx;
				padding: 26rpx 15rpx 8rpx;
				border-bottom: solid 1rpx #D6D6D6;

				view:nth-child(1) {
					font-size: 28rpx;
					font-weight: bold;
				}

				view:nth-child(2) {
					font-size: 18rpx;
					color: #868686;
				}
			}

			.info {
				font-size: 20rpx;
				padding: 14rpx 41rpx;
				border-bottom: solid 1rpx #D6D6D6;
				image,
				video {
					width: 245rpx;
					height: 137rpx;
				}

				.info-item {
					.flex {
						width: 200rpx;
						margin-bottom: 10rpx;

						view:nth-child(2) {
							font-weight: bold;
						}
					}
				}
			}

		}
	}
</style>